﻿using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

namespace ZL_Framework
{
    public enum PlayMode
    {
        SimulateLoad,
        StreamingAssetLoad,
        RemoteLoad
    }

    [StartManager]
    public class AppConfig : MonoSingleton<AppConfig>
    {
        private ProjectSettingScriptableObject config;
        public ProjectSettingScriptableObject Config => config;

        protected override void Init()
        {
            base.Init();
            config = UnityEngine.Resources.Load<ProjectSettingScriptableObject>("ProjectSettingConfig");
        }

        public string ABPath
        {
            get
            {
                string path = "";
                //#if UNITY_EDITOR
                if (config.PlayMode == PlayMode.RemoteLoad)
                {
                    path = Path.Combine(Application.persistentDataPath, "serverassets");
                }
                else
                {
                    path = Path.Combine(Application.streamingAssetsPath, "serverassets");
                }
                //#else
                //                path = Path.Combine(Application.persistentDataPath, "serverassets");
                //#endif
                return path;
            }
        }

        /// <summary>
        /// Lua脚本地址
        /// </summary>
        public string LuaFilePath
        {
            get
            {
                string path = "";
                //#if UNITY_EDITOR
                if (config.PlayMode == PlayMode.SimulateLoad && Application.isEditor)
                {
                    path = Application.dataPath + "/Scripts/LuaScripts/";
                }
                else
                {
                    if (config.PlayMode == PlayMode.RemoteLoad)
                    {
                        path = Application.persistentDataPath + "/serverassets/luascripts/";
                    }
                    else
                    {
                        path = Application.streamingAssetsPath + "/serverassets/luascripts/";
                    }
                }
                //#else
                //                path = Application.persistentDataPath + "/serverassets/luascripts/";

                //#endif
                return path;
            }
        }

        /// <summary>
        /// 检测是否有网
        /// </summary>
        public static bool IsNet
        {
            get
            {
                return GetNetState() != NET_STATE_DISNET;
            }
        }

        /// <summary> 没网 </summary>
        public const int NET_STATE_DISNET = -1;
        /// <summary> 手机数据流量 </summary>
        public const int NET_STATE_MOBILE = 1;
        /// <summary> WIFI/网线 </summary>
        public const int NET_STATE_WIFI = 2;

        /// <summary>
        /// 获取网络状态
        /// </summary>
        /// <returns></returns>
        public static int GetNetState()
        {
            int netCode = NET_STATE_DISNET;
            switch (Application.internetReachability)
            {
                case NetworkReachability.NotReachable:
                    netCode = NET_STATE_DISNET;
                    break;
                case NetworkReachability.ReachableViaCarrierDataNetwork:
                    netCode = NET_STATE_MOBILE;
                    break;
                case NetworkReachability.ReachableViaLocalAreaNetwork:
                    netCode = NET_STATE_WIFI;
                    break;
                default:
                    netCode = NET_STATE_DISNET;
                    break;
            }
            return netCode;
        }
    }
}